package cn.haoma.base.common;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;

import java.io.File;

import cn.haoma.base.R;
import cn.haoma.base.utils.IntentUtil;

/**
 * Created by ilioili on 2015/6/10.
 */
public abstract class ChoosePicActivity extends BaseActivity {
    protected static final int REQUEST_CODE_SELECT_PIC_FROME_CAMERA = 11;
    protected static final int REQUEST_CODE_SELECT_PIC_FROME_GALLERY = 12;
    protected static final int REQUEST_CODE_CROP_PIC = 13;
    private File cropFile;
    private File picFile;
    private int reqId;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_FINISH:
                    setResult(RESULT_OK);
                    finish();
                    break;
                case REQUEST_CODE_SELECT_PIC_FROME_CAMERA:
                    onPicSelected(picFile, reqId);
                    break;
                case REQUEST_CODE_SELECT_PIC_FROME_GALLERY:
                    String tmpFilePath = IntentUtil.getPicPath(this, data);
                    onPicSelected(new File(tmpFilePath), reqId);
                    break;
                case REQUEST_CODE_CROP_PIC:
                    onCropPicSelected(cropFile, reqId);
                    break;
            }
        }

    }

    protected abstract void onPicSelected(File file, int reqId);

    protected abstract void onCropPicSelected(File cropFile, int reqId);

    /**
     * 调用第三方图库剪切图片，onCropPicSelected(File file, int reqId)回调
     *
     * @param srcFile
     * @param dstFile 图片位置应该是在SD卡
     * @param reqId
     */
    protected void selectCropPic(File srcFile, File dstFile, int reqId) {
        this.reqId = reqId;
        this.cropFile = dstFile;
        startActivityForResult(IntentUtil.getCaptureIntent(srcFile, dstFile), REQUEST_CODE_CROP_PIC);
    }

    /**
     * @param picFile 拍照产生的照片要存储的地方
     */
    protected void showPicDialog(final File picFile, final int reqId) {
        final Dialog dialog = new Dialog(this);
        Window window = dialog.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setBackgroundDrawable(new ColorDrawable(0));
        dialog.setContentView(R.layout.dialog_choose_pic);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (v.getId() == R.id.bt_camera) {
                    selectPicFromCamera(picFile, reqId);
                } else {
                    selectPicFromGallery(reqId);
                }
                //                switch (v.getId()) {
                //                    case R.id.bt_camera:
                //
                //                        break;
                //                    case R.id.bt_gallery:
                //
                //                        break;
                //                }

            }
        };
        window.findViewById(R.id.bt_camera).setOnClickListener(listener);
        window.findViewById(R.id.bt_gallery).setOnClickListener(listener);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    /**
     * 拍照获取图片，成功后从onPicSelected(File file, int reqId)回调
     *
     * @param file  生成的照片存放的文件
     * @param reqId
     */
    protected void selectPicFromCamera(File file, int reqId) {
        this.reqId = reqId;
        this.picFile = file;
        startActivityForResult(IntentUtil.getPicFromCameraIntent(file.getPath()), REQUEST_CODE_SELECT_PIC_FROME_CAMERA);
    }

    /**
     * 从图库选择一个图片，成功后从onPicSelected(File file, int reqId)回调
     *
     * @param reqId
     */
    protected void selectPicFromGallery(int reqId) {
        this.reqId = reqId;
        startActivityForResult(IntentUtil.getPicIntent(), REQUEST_CODE_SELECT_PIC_FROME_GALLERY);
    }

}
